package com.yidu.labyrinth.dao;

import com.yidu.labyrinth.main.LabyrinthRoleEntity;
import com.yidu.labyrinth.main.LabyrinthRoleOrUserEntity;
import com.yidu.labyrinth.main.LabyrinthUserEntity;
import com.yidu.labyrinth.vo.Pages;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.util.StringUtils;
import java.util.List;

/**
 * @描述：
 * @ClassName:labyrinth
 * @backage:com.yidu.labyrinth.dao
 * @Date:2019 2019/9/25 13:33
 * @Author:易嵘
 **/
@SuppressWarnings("ALL")
public class LabyrinthUserDaoImpl implements LabyrinthUserDao {
    Logger logger=Logger.getLogger(LabyrinthUserDaoImpl.class);

    private HibernateTemplate hibernateTemplate;

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;

    }

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    @Override
    public int update(LabyrinthUserEntity labyrinthUserEntity) {
        try {
            Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
            logger.info(labyrinthUserEntity.getUserName()+"=======================userName-------------------");
            logger.info(labyrinthUserEntity.getUserId()+"=======================userId-------------------");
            session.update(labyrinthUserEntity);
            logger.info("hujiafu");

        }catch (HibernateException e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    @Override
    public int add(LabyrinthUserEntity labyrinthUserEntity) {
        try {
            hibernateTemplate.save(labyrinthUserEntity);
        }catch (HibernateException e){
            e.printStackTrace();
            return 0;
        }

        return 1;
    }

    @Override
    public int addUserOrRole(LabyrinthRoleOrUserEntity labyrinthRoleOrUserEntity) {
        try {
            hibernateTemplate.save(labyrinthRoleOrUserEntity);
        }catch (HibernateException e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    /**
     * 分页查询所有
     * @param labyrinthUserEntity
     * @param pages
     * @return
     */
    @Override
    public List<LabyrinthUserEntity> findAll(LabyrinthUserEntity labyrinthUserEntity, Pages pages) {
        DetachedCriteria criteria = DetachedCriteria.forClass(LabyrinthUserEntity.class);
        if(!StringUtils.isEmpty(labyrinthUserEntity.getUserName())){
            criteria.add(Restrictions.ilike("userName","%"+labyrinthUserEntity.getUserName()+"%"));
        }
        criteria.addOrder(Order.asc("userSort"));
        return (List<LabyrinthUserEntity>) hibernateTemplate.findByCriteria(criteria,pages.getOutPage(),pages.getLimit());
        //List<LabyrinthUserEntity> list=(List<LabyrinthUserEntity>)hibernateTemplate.find("from LabyrinthUserEntity");
        //return list;
    }

    /**
     * 单个查询
     * @param id
     * @return
     */
    @Override
    public LabyrinthUserEntity findById(String id) {
        LabyrinthUserEntity labyrinthUserEntity=hibernateTemplate.load(LabyrinthUserEntity.class,id);
        return labyrinthUserEntity;
    }

    @Override
    public Long userCount(LabyrinthUserEntity labyrinthUserEntity) {
        DetachedCriteria criteria = DetachedCriteria.forClass(LabyrinthUserEntity.class);
        criteria.setProjection(Projections.rowCount());
        if(!StringUtils.isEmpty(labyrinthUserEntity.getUserName())){
            criteria.add(Restrictions.ilike("userName","%"+labyrinthUserEntity.getUserName()+"%"));
        }
        return (Long) hibernateTemplate.findByCriteria(criteria).get(0);
    }

    @Override
    public List<LabyrinthRoleEntity> findAllRole() {
        List<LabyrinthRoleEntity> labyrinthRoleEntityList= (List<LabyrinthRoleEntity>) hibernateTemplate.find("from LabyrinthRoleEntity where roleResult=?0",1);
        return labyrinthRoleEntityList;
    }

    @Override
    public List<LabyrinthRoleOrUserEntity> findAllUserOrRole(String id) {
        DetachedCriteria criteria = DetachedCriteria.forClass(LabyrinthRoleOrUserEntity.class);
        criteria.add(Restrictions.eq("labyrinthUserEntity.userId",id));
        return (List<LabyrinthRoleOrUserEntity>) hibernateTemplate.findByCriteria(criteria);
    }

    @Override
    public int deleteUserOrRole(String id) {
        try {
        //hibernateTemplate.find("delete from LabyrinthRoleOrUserEntity la where la.labyrinthUserEntity.userId=?0",id);
            Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
            Query query = session.createSQLQuery("delete from LABYRINTH_ROLE_OR_USER  where USER_ID=?0");
            query.setParameter(0,id);
            query.executeUpdate();
        }catch (HibernateException e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    @Override
    public LabyrinthUserEntity login(String userName, String userPwd) {
        DetachedCriteria criteria = DetachedCriteria.forClass(LabyrinthUserEntity.class);
        criteria.add(Restrictions.eq("userName",userName));
        criteria.add(Restrictions.eq("userPwd",userPwd));
        criteria.add(Restrictions.eq("userResult",1));
        LabyrinthUserEntity labyrinthUserEntity= (LabyrinthUserEntity) hibernateTemplate.findByCriteria(criteria);
        return labyrinthUserEntity;
    }


}

